<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="工法名称" prop="gfName">
        <el-input size="mini"
                  v-model="queryParams.gfName"
                  placeholder="请输入工法名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="nd">
        <el-input size="mini" type="number"
                  v-model="queryParams.nd"
                  placeholder="请输入年度"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工法类别" prop="gfLb">
        <el-select @change="getZyFlQuery" size="mini" v-model="queryParams.gfLb" placeholder="请选择工法类别" clearable>
          <el-option
            v-for="dict in dict.type.qygf_gflb"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业分类" prop="zyFl">
        <el-select v-if="queryParams.gfLb == null" size="mini" v-model="queryParams.zyFl" placeholder="请先选择工法类别"
                   clearable>
        </el-select>
        <el-select v-if="queryParams.gfLb == '房屋建筑工程'" size="mini" v-model="queryParams.zyFl"
                   placeholder="请选择专业分类" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyfl_fwjzgc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select v-if="queryParams.gfLb == '土木工程'" size="mini" v-model="queryParams.zyFl"
                   placeholder="请选择专业分类" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyfl_tmgc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select v-if="queryParams.gfLb == '工业安装工程'" size="mini" v-model="queryParams.zyFl"
                   placeholder="请选择专业分类" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyfl_gyazgc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申报级别" prop="sbJb">
        <el-select size="mini" v-model="queryParams.sbJb" placeholder="请选择申报级别" clearable>
          <el-option
            v-for="dict in dict.type.qygf_sbjb"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工法完成人" prop="zyWcR">
        <el-input size="mini"
                  v-model="queryParams.zyWcR"
                  placeholder="请输入工法完成人"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报单位" prop="sbDw">
        <el-select size="mini" v-model="queryParams.sbDw" placeholder="请选择申报单位" clearable>
          <el-option
            v-for="dict in dict.type.xmgl_lxwj_pzdw"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业领域" prop="zyLy">
        <el-select size="mini" v-model="queryParams.zyLy" placeholder="请选择专业领域" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyly"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.qygf_lczt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kjcg:qygf:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="qygfList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="年度" align="center" prop="nd" width="70"/>
      <el-table-column label="工法名称" align="center" prop="gfName">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.gfName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="工法编号" align="center" prop="gfBh" />
      <el-table-column label="申报级别" align="center" prop="sbJb">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qygf_sbjb" :value="scope.row.sbJb"/>
        </template>
      </el-table-column>
      <el-table-column label="工法类别" align="center" prop="gfLb">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qygf_gflb" :value="scope.row.gfLb"/>
        </template>
      </el-table-column>
      <el-table-column label="专业分类" align="center" prop="zyFl"/>
      <el-table-column label="工法完成人" align="center" prop="zyWcR"/>
      <el-table-column label="申报单位" align="center" prop="sbDw">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xmgl_lxwj_pzdw" :value="scope.row.sbDw"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="processStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qygf_lczt" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查询进度 -->
    <Jindu ref="picture"/>

    <!-- 查询进度 -->
    <QyGfXq ref="qyGfXq"/>
  </div>
</template>

<script>
import {
  deleteQygfFjByFjIds,
  listQygf,
  selectQygfFjList,
} from "@/api/kjcg/qygf";
import KjryDuoXName from "@/views/components/kjryXz/kjryDuoXName.vue";
import HtXzKt from "@/views/components/ktXz/HtXzKt.vue";
import Jindu from "@/views/components/liuChengState/jindu.vue";
import QyGfXq from "@/views/kjcg/qygfsb/components/QyGfXq.vue";

export default {
  name: "Qygfsb",
  components: {QyGfXq, Jindu, HtXzKt, KjryDuoXName},
  //企业工法申报
  dicts: ['xmgl_lxwj_pzdw', 'qygf_sbjb', 'qygf_zyly', 'qygf_gflb', 'qygf_zyfl_fwjzgc', 'qygf_zyfl_tmgc', 'qygf_zyfl_gyazgc', 'qygf_lczt'],
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQygfFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业工法表格数据
      qygfList: [],
      // 企业工法附件表格数据
      qygfFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gfName: null,
        nd: null,
        gfLb: null,
        zyFl: null,
        sbJb: null,
        zyWcR: null,
        kjryId: null,
        sbDw: null,
        zyLy: null,
        xmsbId: null,
        kyXm: null,
        idWhere: null,
        userId: null,
        deptId: null,
        processStatus: null,
        gfBh: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //打开企业工法详情
    handleButtonClick(row) {
      this.$refs.qyGfXq.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    //当搜索栏中更换工法类别时, 重置专业分类
    getZyFlQuery() {
      this.queryParams.zyFl = null
    },
    //当表单中更换工法类别时, 重置专业分类
    getFormByGfFl() {
      this.form.zyFl = null
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询企业工法列表 */
    getList() {
      this.loading = true;
      listQygf(this.queryParams).then(response => {
        this.qygfList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qygfId: this.getQyGfId,
        gfName: null,
        nd: new Date().getFullYear(),
        gfLb: null,
        zyFl: null,
        sbJb: null,
        zyWcR: null,
        kjryId: null,
        lxr: null,
        lxDh: null,
        lxQq: null,
        sbDw: null,
        zyLy: null,
        xmsbId: null,
        kyXm: null,
        qy: null,
        gfTd: null,
        syFw: null,
        gyYl: null,
        sggylcCzyd: null,
        clSb: null,
        zlKz: null,
        aqCs: null,
        hbCs: null,
        xyFx: null,
        yySl: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,
        gfBh: null
      };
      this.qygfFjList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.qygfId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 企业工法附件序号 */
    rowQygfFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 企业工法附件删除按钮操作 */
    handleDeleteQygfFj() {
      if (this.checkedQygfFj.length == 0) {
        this.$modal.msgError("请先选择要删除的企业工法附件数据");
      } else {
        const fjIds = this.checkedQygfFj;
        this.$modal.confirm('确定删除选中记录？').then(function() {
          return deleteQygfFjByFjIds(fjIds);
        }).then(() => {
          this.queryParamsFj.qygfId = this.qygfIdEdit || this.getQyGfId
          this.queryParamsFj.fileType = this.qygfFileType
          selectQygfFjList(this.queryParamsFj).then(resFileList => {
            this.qygfFjList = resFileList.rows;
            console.log(this.qygfFjList, 'dsaadsdasd')
            this.loadingFj = false
          })
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 复选框选中数据 */
    handleQygfFjSelectionChange(selection) {
      this.checkedQygfFj = selection.map(item => item.fjId)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kjcg/qygf/export', {
        ...this.queryParams
      }, `qygf_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
